package com.smartwasser.yunhtgl.apphtgl.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.adapter.MainAdapter;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BasePager;
import com.smartwasser.yunhtgl.apphtgl.pager.HPager;
import com.smartwasser.yunhtgl.apphtgl.pager.MPager;
import com.smartwasser.yunhtgl.apphtgl.widget.CustomToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Home
 */
public class MainActivity extends BaseActivity {
    private List<BasePager> mListPager;
    @BindView(R.id.pager_home)
    ViewPager pager_home;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.group_home)
    RadioGroup group_home;
    @BindView(R.id.pager_h)
    RadioButton pager_h;
    @BindView(R.id.pager_m)
    RadioButton pager_m;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("合同服务管理系统");
        back.setVisibility(View.INVISIBLE);
        pager_h.setOnClickListener(this);
        pager_m.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mListPager = new ArrayList<>();
        mListPager.add(new HPager(this));
        mListPager.add(new MPager(this));
        pager_home.setAdapter(new MainAdapter(mListPager));
        pager_home.setOnPageChangeListener(new MyPageChangeListener());
        pager_home.setCurrentItem(0);
        group_home.check(R.id.pager_h);
        pager_h.setTextColor(getResources().getColor(R.color.colorShadow));
    }

    @OnClick({R.id.pager_2})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pager_h:
                pager_home.setCurrentItem(0);
                break;
            case R.id.pager_2:
                Intent intent = new Intent(this, SysActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.pager_m:
                pager_home.setCurrentItem(1);
                break;
        }

    }

    class MyPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    pager_home.setCurrentItem(0);
                    tv_toolbar.setText("合同服务管理系统");
                    group_home.check(R.id.pager_h);
                    pager_h.setTextColor(getResources().getColor(R.color.colorShadow));
                    pager_m.setTextColor(getResources().getColor(R.color.colorWhite));
                    break;
                case 1:
                    pager_home.setCurrentItem(1);
                    tv_toolbar.setText("个人");
                    group_home.check(R.id.pager_m);
                    pager_m.setTextColor(getResources().getColor(R.color.colorShadow));
                    pager_h.setTextColor(getResources().getColor(R.color.colorWhite));
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                CustomToast.showToast(this, "再按一次退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}


